package sorted;

import java.util.Arrays;

public class S03_InsertionSort {

    public static void main(String[] args) {
        int[] arr = {5,7,2,3,4,1,6,18,9,10,16,56,34,23,11,12,31,17,15};
        int[] sorted = insertionSort(arr);

        System.out.println("排序后的数组为："+ Arrays.toString(sorted));
    }

    public static int[] insertionSort(int[] arr) {
        int length = arr.length;
        for (int i = 1; i < length; i++) {
            for (int j = i; j > 0; j--) {
                if (arr[j] > arr[j - 1])break;
                int temp = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = temp;
            }
        }

        return arr;
    }
}


